package gp;

import java.util.ArrayList;
import java.util.Stack;

public class Individuo {
	float fitness;
	Tree tree;
	
	
	public Individuo(float fitness, Tree tree){
		this.fitness = fitness;
		this.tree = tree;
	}
	
	public Individuo(float fitness, Stack<Symbol> symbolsStack){
		this.fitness = fitness;
		this.tree = new Tree(symbolsStack);
	}
	
	public Node get_root(){
		return this.tree.root;
	}
	
	public int get_height(){
		return this.tree.getHeight();
	}
	
	public ArrayList<Symbol> get_vector_height(int max_height){
		ArrayList<Symbol> vector = new ArrayList<Symbol>();
		tree.get_vector_height(max_height, vector);
		return vector;
	}
	
	private double calculate_order(ArrayList<Symbol> vector, int alphabet_size){
		double order = 0;
		for(int i = 0 ; i < vector.size(); i++){
			order += vector.get(i).getValor() * Math.pow(alphabet_size, vector.size() - i -1);
		}
		return order;
	}
	
	public double calculate_angle(Alphabet alphabet){
		int height = this.tree.getHeight();
		double angulo = 0;
		double ordem = 0;
		ArrayList<Symbol> word = new ArrayList<Symbol>();
		for(int i = 0; i <= height; i++){
			word.addAll(this.get_vector_height(i));
		}
		
		/*
		ArrayList<Symbol> vector = this.get_vector_height(0);
		double subdivisao = 360/(alphabet.size()-1);
		double inicio_arco = vector.get(0).getValor() * subdivisao;
		double fim_arco = (vector.get(0).getValor()+1) * subdivisao;
		
		for(int i = 1; i <= height; i++){
			inicio_arco = calculate_angle(alphabet, i) + inicio_arco ;
		}
		*/
		
		double order = calculate_order(word, alphabet.size());
		
		/*
		for(int i = 0; i < word.size(); i += 1){
			angulo = word.get(i).getValor() * Math.pow(alphabet.size(), word.size() - i);
		}
		*/
		double alphabet_size = alphabet.size();
		double word_size = word.size();
		double  constante = 360 / (( alphabet_size - 1) * Math.pow(alphabet_size, word_size-1));
		return constante * order;
		
	}
	
	public double calculate_angle(Alphabet alphabet, int height){
		ArrayList<Symbol> vector = this.get_vector_height(height);
		double subdivisao = (360/alphabet.size()-1)/Math.pow(alphabet.size(),(Math.pow(2, height)));
		double order = calculate_order(vector, alphabet.size());
		return subdivisao * order;
	}
}
